const cloud = require('wx-server-sdk');
cloud.init({ env: 'cloud1-2g029zan6f8e16b1' }); // 替换为你的环境ID
const db = cloud.database();

exports.main = async (event, context) => {
  const { fromUser, toUser, content, type = 'text' } = event;
  const timestamp = new Date().getTime();

  try {
    await db.collection('messages').add({
      data: {
        fromUser,
        toUser,
        content,
        type,
        timestamp,
        isRead: false
      }
    });
    return { success: true };
  } catch (err) {
    console.error('保存消息失败', err);
    return { success: false, errMsg: err.message };
  }
};